package io.geekidea.boot.webStock;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Slf4j
@Component
public class WebSocketServer {

    private final ServerBootstrap serverBootstrap;
    private Channel serverChannel;

    @Value("${websocket.port:8880}")
    private int port;

    public WebSocketServer(ServerBootstrap serverBootstrap) {
        this.serverBootstrap = serverBootstrap;
    }

    @PostConstruct
    public void start() throws Exception {
        try {
            serverChannel = serverBootstrap.bind(port).sync().channel();
            log.info("WebSocket server started on port: {}", port);
        } catch (Exception e) {
            log.error("Failed to start WebSocket server", e);
            throw e;
        }
    }

    @PreDestroy
    public void stop() {
        if (serverChannel != null) {
            serverChannel.close();
            log.info("WebSocket server stopped");
        }
    }
} 